
package com.accenture.adf.newcodington.module18.sample;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * 
 * Topic: Demo serialzing and deserializing a Person object
 * 
 * Instructions: 
 * Store the Person data into a .dat file
 * 
 * 1. Create a new instance of the Person class and set the relevant data.<br/>
 * 2. Set the objPerson instance with data.<br/>
 * 3. Create a file (Person.dat) and write the Person details<br/>
 *    a. Utilize the FileOutputStream and ObjectOutputStream for this purpose
 * 
 */

public class SerialFileDemo {

	public static void main(String[] args) {
		// Create a new instance of the person class and set the relevant data.
		Person objPerson = new Person();
		objPerson.setName("ADFJava");
		objPerson.setAddress("Accenture Technologies");
		objPerson.setNumber(101);
		try {
			System.out.println("Before Serialization");
			//Create a file (Person.dat) and write the Person details
			FileOutputStream fileOut = new FileOutputStream("Person.dat");		//Cargo todo el ficero (person.dat)
			ObjectOutputStream out = new ObjectOutputStream(fileOut);			//Para poderlo manipular de mejor manera, creo el objeto ObjectOutputStream
			out.writeObject(objPerson);											//Lo serializamos (lo hemos leido en cod.binario(el .bat), y ahora lo pasamos a String)
			out.close();														//Hay que cerrarlo (lo ha estado metiendo en bloques (empaquetando))
			fileOut.close();													//Tambien lo cerramos
			System.out.println("End of Serialization");
		} catch (IOException i) {
			i.printStackTrace();
		}

	}

}
